<?php

namespace app\admin\model;

use think\Model;

class Customer extends Model
{
    // 表名
    protected $name = 'customer';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    
    // 追加属性
    protected $append = [
        'scope_text',
        'type_text',
        'level_text',
        'status_text',

    ];

    #protected $hidden = ['createtime','updatetime'];


    protected static function init(){
        self::afterInsert(function($row){
            self::doAfterInsert($row);
        });

        self::afterUpdate(function ($row){
            self::doAfterUpdate($row);

        });
    }

    private static function doAfterInsert($row){
        //写入公司id
        $user = \think\Db::name('user')->where('id',$row['user_id'])->find();
        \think\Db::name('customer')->where('id',$row['id'])->update(['company_id' => $user['company_id']]);

        $scope = self::getScopeList();
        $record = '由'. $user['nickname'] . '主动加入，并设置为' . $scope[$row['scope']];
        Visitlog::create([
            'customer_id' => $row['id'],
            'user_id' => $row['user_id'],
            'record' => $record
        ]);

        //记录添加者
        $redis = new \Predis\Client(\think\Config::get('redis'));
        $key = 'customer_creator:' .$row['user_id'];
        $redis->lpush($key,$row['id']);

    }

    private static function doAfterUpdate($row){
        $user = \think\Db::name('user')->where('id',$row['user_id'])->find();

        $record1 = $user['nickname'] . '将客户共享设置为：' . $row['scope_text'];
        $record2 = $user['nickname'] . '为客户打上标签：' . $row['type_text'] . ' ' . $row['level_text'] . ' ' .$row['status_text'] ;
        $data = [
                    ['customer_id' => $row['id'],'user_id' => $row['user_id'],'record' => $record1],
                    ['customer_id' => $row['id'],'user_id' => $row['user_id'],'record' => $record2],
            ];
        $visitlogM = new Visitlog();
        $visitlogM->saveAll($data);

    }




    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = Tags::getTagsByType(2,$data['company_id']);
        $list = list2kv($list);
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getLevelTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['level']) ? $data['level'] : '');
        $list = Tags::getTagsByType(1,$data['company_id']);
        $list = list2kv($list);

        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = Tags::getTagsByType(0,$data['company_id']);
        $list = list2kv($list);

        return isset($list[$value]) ? $list[$value] : '';
    }

    public static function getScopeList()
    {
        return ['0' => __('Scope 0'),'1' => __('Scope 1')];
    }     


    public function getScopeTextAttr($value, $data)
    {        
        $value = $value ? $value : (isset($data['scope']) ? $data['scope'] : '');
        $list = $this->getScopeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function user(){
        return $this->belongsTo('User','user_id','id')->setEagerlyType(0);
    }

    public function company(){
        return $this->belongsTo('Company','company_id','id')->setEagerlyType(0);
    }

    public function visitlog(){
        return $this->hasMany('Visitlog')->order('id desc');
    }

    




}
